package com.demo.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.demo.entity.constants.Constants;
import com.demo.entity.enums.GroupStatusEnum;
import com.demo.entity.enums.ResponseCodeEnum;
import com.demo.entity.po.GroupInfo;

import com.demo.entity.po.GroupMember;
import com.demo.entity.query.GroupInfoQuery;

import com.demo.entity.query.GroupMemberQuery;
import com.demo.entity.vo.PaginationResultVO;

import com.demo.exception.BusinessException;
import com.demo.mappers.GroupMemberMapper;
import com.demo.service.GroupInfoService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.demo.entity.query.SimplePage;

import com.demo.mappers.GroupInfoMapper;

import com.demo.entity.enums.PageSize;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:小组表serviceImpl
 * @Date:2025-03-01
 * @author：author
*/
@Service("groupInfoService")
public class GroupInfoServiceImpl implements GroupInfoService{

	@Resource
	private GroupInfoMapper<GroupInfo,GroupInfoQuery> groupInfoMapper;

	@Resource
	private GroupMemberMapper<GroupMember, GroupMemberQuery> groupMemberMapper;


	/**
	 * 根据条件查询列表
	 */
	public List<GroupInfo> findListByParam(GroupInfoQuery query) { 
		return this.groupInfoMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(GroupInfoQuery query){
		return this.groupInfoMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<GroupInfo> findListByPage(GroupInfoQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<GroupInfo> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}




	/**
	 * 批量新增
	 */
	public Integer addBatch(List<GroupInfo> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.groupInfoMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<GroupInfo> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.groupInfoMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据GroupId查询
	 */
	public GroupInfo getGroupInfoByGroupId(Long groupId) { 
		return this.groupInfoMapper.selectByGroupId(groupId);
	}

	/**
	 * 根据GroupId更新
	 */
	public Integer updateGroupInfoByGroupId(GroupInfo bean, Long groupId) { 
		return this.groupInfoMapper.updateByGroupId(bean,groupId);
	}

	/**
	 * 根据GroupId删除
	 */
	public Integer deleteGroupInfoByGroupId(Long groupId) { 
		return this.groupInfoMapper.deleteByGroupId(groupId);
	}


	/**
	 * 讨论群组信息
	 */

	@Transactional(rollbackFor = Exception.class)
	public Integer add(GroupInfo bean) {
		Date curDate = new Date();
		bean.setStatus(GroupStatusEnum.ENABLE.getStatus());
		bean.setCreateTime(curDate);
		bean.setUpdateTime(curDate);
		Integer insert = this.groupInfoMapper.insert(bean);
		GroupMember groupMember = new GroupMember();
		groupMember.setGroupId(bean.getGroupId());
		groupMember.setUserId(bean.getCreatorId());
		groupMember.setJoinTime(curDate);
		groupMember.setRole(Constants.ONE);
		groupMemberMapper.insert(groupMember);
		return insert;
    }

	/**
	 * 保存信息
	 * @param bean
	 */
	@Override
	public void updateInfo(GroupInfo bean) {
		// 未传id
		if (bean.getGroupId() == null) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		GroupInfo groupInfoDB = groupInfoMapper.selectByGroupId(bean.getGroupId());
		// 拦截非法小组，或者更新别人的小组
		if (groupInfoDB == null||!groupInfoDB.getCreatorId().equals(bean.getCreatorId())) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		Date date = new Date();
		bean.setUpdateTime(date);
		groupInfoMapper.updateByGroupId(bean,bean.getGroupId());
	}

	/**
	 * 根据条件删除小组信息
	 * 用户id 小组id
	 * @param groupInfo
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteByParam(GroupInfoQuery groupInfo) {
		groupInfo.setStatus(GroupStatusEnum.ENABLE.getStatus());
		Integer count = groupInfoMapper.deleteByParam(groupInfo);
		if (count<= Constants.ZERO){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
	}

	@Override
	public PaginationResultVO<GroupInfo> findMyJoinGroup(GroupInfoQuery query) {
		Long currentUserId = query.getCreatorId();
		GroupMemberQuery memberQuery = new GroupMemberQuery();
		memberQuery.setUserId(currentUserId);
		int count = groupMemberMapper.selectCount(memberQuery);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		memberQuery.setSimplePage(page);
		List<GroupMember> myJoinedGroup =groupMemberMapper.selectList(memberQuery);


		List<GroupInfo> groupInfoList = new ArrayList<>();
		// 查询我创建的群组
		GroupInfoQuery groupInfoQuery=new GroupInfoQuery();
		groupInfoQuery.setCreatorId(currentUserId);
		List<GroupInfo> myGroupIds = groupInfoMapper.selectList(groupInfoQuery);
		List<Long> myGroupIdList = myGroupIds.stream().map(GroupInfo::getGroupId).collect(Collectors.toList());
		myJoinedGroup.forEach(m->{
			if (!myGroupIdList.contains(m.getGroupId())){
				GroupInfo groupInfo = groupInfoMapper.selectByGroupId(m.getGroupId());
				groupInfoList.add(groupInfo);
			}
		});

		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),groupInfoList);
	}
}